package org.black.phoneUtility.sms.reply.app.fragment;

import java.util.Calendar;

import org.black.phoneUtility.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;

public class SmsReplyReplyDateTimePeriodTimePickerDialogFragment extends
		DialogFragment {

	public static final String TAG = SmsReplyReplyDateTimePeriodTimePickerDialogFragment.class
			.getName();
	public static final String ARGUMENTS_IS_BEGIN = "is_begin";
	public static final String ARGUMENTS_HOUR_OF_DAY = "hourOfDay";
	public static final String ARGUMENTS_MINUTE = "minute";

	public interface SmsReplyReplyDateTimePeriodTimePickerInterface {
		public void onReplyDateTimePeriodTimePickerDialogPositiveClick(
				boolean isBegin, int hourOfDay, int minute);

		public void onReplyDateTimeRepeatTimePickerDialogNegativeClick();
	}

	private SmsReplyReplyDateTimePeriodTimePickerInterface smsReplyReplyDateTimePeriodTimePickerInterface;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			smsReplyReplyDateTimePeriodTimePickerInterface = (SmsReplyReplyDateTimePeriodTimePickerInterface) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(
					activity.toString()
							+ " must implement SmsReplyReplyDateTimePeriodTimePickerDialogFragment");
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		super.onCreateDialog(savedInstanceState);
		Bundle bundle = getArguments();
		final boolean isBegin = bundle.getBoolean(ARGUMENTS_IS_BEGIN);
		int hourOfDay = bundle.getInt(ARGUMENTS_HOUR_OF_DAY);
		int minute = bundle.getInt(ARGUMENTS_MINUTE);
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

		LayoutInflater inflater = getActivity().getLayoutInflater();
		View view = inflater.inflate(
				R.layout.sms_reply_reply_date_time_period_time_picker_dialog,
				null);
		final TimePicker smsReplyDateTimePeriodTimePickerTimePicker = (TimePicker) view
				.findViewById(R.id.smsReplyDateTimePeriodTimePickerTimePicker);
		boolean is24HourView = DateFormat.is24HourFormat(getActivity());
		smsReplyDateTimePeriodTimePickerTimePicker
				.setIs24HourView(is24HourView);
		smsReplyDateTimePeriodTimePickerTimePicker.setCurrentHour(hourOfDay);
		smsReplyDateTimePeriodTimePickerTimePicker.setCurrentMinute(minute);
		Button smsReplyDateTimePeriodTimePickerRightNowButton = (Button) view
				.findViewById(R.id.smsReplyDateTimePeriodTimePickerRightNowButton);
		smsReplyDateTimePeriodTimePickerRightNowButton
				.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						Calendar calendar = Calendar.getInstance();
						int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);
						int minute = calendar.get(Calendar.MINUTE);
						smsReplyDateTimePeriodTimePickerTimePicker
								.setCurrentHour(hourOfDay);
						smsReplyDateTimePeriodTimePickerTimePicker
								.setCurrentMinute(minute);
					}
				});

		builder.setView(view)
				.setPositiveButton(android.R.string.ok,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								smsReplyReplyDateTimePeriodTimePickerInterface
										.onReplyDateTimePeriodTimePickerDialogPositiveClick(
												isBegin,
												smsReplyDateTimePeriodTimePickerTimePicker
														.getCurrentHour(),
												smsReplyDateTimePeriodTimePickerTimePicker
														.getCurrentMinute());

							}
						})
				.setNegativeButton(android.R.string.cancel,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {

							}
						});
		return builder.create();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

}
